package crowx.android.AndCellTrack;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.Location;
import android.location.LocationManager;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.telephony.CellLocation;
import android.telephony.PhoneStateListener;
import android.telephony.ServiceState;
import android.telephony.TelephonyManager;
import android.telephony.gsm.GsmCellLocation;

abstract class AndCellTrackServiceBase extends Service {
	protected static final String TAG = "AndCellTrack";
	protected final CellInfo cellInfo = new CellInfo();
	protected final CellTrackStateObservable cellTrackStateObservable = new CellTrackStateObservable(cellInfo);
	private static final int AND_CELL_TRACK_SERVICE_ID = 1;
	
	protected TelephonyManager telephonyManager = null;
	private NotificationManager notificationManager = null;
	private LocationManager locationManager = null;
	private SharedPreferences preferences = null;
	
	@Override
	public final IBinder onBind(Intent intent) {
		return cellTrackStateObservable;
	}
	
	@Override
	public void onCreate() {
		super.onCreate();
		android.util.Log.i(TAG, "AndCellTrackServiceBase.onCreate");
		
		preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
		preferences.registerOnSharedPreferenceChangeListener(preferenceChangeListener);
		
		if(preferences.getBoolean("use_gps",true)) {
			locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
			//locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 500, 0, locationListener);
		}
		
		notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
		
		telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE); 
		telephonyManager.listen(stateListener,  PhoneStateListener.LISTEN_SIGNAL_STRENGTH | PhoneStateListener.LISTEN_CELL_LOCATION | PhoneStateListener.LISTEN_SERVICE_STATE);
    	
		//broadcastReciver = new SMSBroadcastReciver();
		//registerReceiver(broadcastReciver,new IntentFilter("android.provider.Telephony.SMS_RECEIVED"));
		
		showNotification(TAG,false);    	
	}
	
	@Override
	public void onDestroy() {
		super.onDestroy();
		android.util.Log.i(TAG, "AndCellTrackServiceBase.onDestroy");
		
		preferences.unregisterOnSharedPreferenceChangeListener(preferenceChangeListener);
		telephonyManager.listen(stateListener, PhoneStateListener.LISTEN_NONE);
		
		//unregisterReceiver(broadcastReciver);

		cancelNotification();
	}
	
	protected void showNotification(CharSequence contentText, boolean addNewBtsNotification) {
		final int icon = android.R.drawable.stat_notify_more;
		final Context context = getApplicationContext();
		final CharSequence contentTitle = context.getResources().getString(R.string.app_name);
		
		Intent notificationIntent = new Intent(context, AndCellTrack.class);
		PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

		Notification notification = new Notification(icon, null, 0);
		notification.flags = Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT;
		if(addNewBtsNotification) notification.defaults |= Notification.DEFAULT_SOUND;
		notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
		notificationManager.notify(AND_CELL_TRACK_SERVICE_ID, notification);
	}
	
	private void cancelNotification() {
		notificationManager.cancel(AND_CELL_TRACK_SERVICE_ID);
	}
	
	protected Location getGPSLocation() {
		if(locationManager != null) {
			return locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
		}
		return null;
	}
	
	protected abstract void onCellLocationChanged(GsmCellLocation location, int networkType);		
	protected abstract void onServiceStateChanged(ServiceState serviceState, GsmCellLocation location, int networkType);
	protected abstract void onSignalStrengthChanged(int asu);
	
	private final PhoneStateListener stateListener = new PhoneStateListener() {			
		@Override
		public void onCellLocationChanged(CellLocation location) {
			super.onCellLocationChanged(location);
			AndCellTrackServiceBase.this.onCellLocationChanged((GsmCellLocation)location,telephonyManager.getNetworkType());
		}		
		
		@Override
		public void onServiceStateChanged(ServiceState serviceState) {
			super.onServiceStateChanged(serviceState);
			GsmCellLocation location = (GsmCellLocation)telephonyManager.getCellLocation();
			AndCellTrackServiceBase.this.onServiceStateChanged(serviceState,location,telephonyManager.getNetworkType());
		}
		
		@Override
		public void onSignalStrengthChanged(int asu) {
			super.onSignalStrengthChanged(asu);
			AndCellTrackServiceBase.this.onSignalStrengthChanged(asu);
		}
	};
	
	//protected abstract void onLocationChanged(Location location);
	
	/*private final LocationListener locationListener = new LocationListener() {
		@Override
		public void onLocationChanged(Location location) {
			AndCellTrackServiceBase.this.onLocationChanged(location);
		}

		@Override
		public void onProviderDisabled(String arg0) {
		}
		@Override
		public void onProviderEnabled(String arg0) {
		}
		@Override
		public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
		}  	
    };*/
    
    private final SharedPreferences.OnSharedPreferenceChangeListener preferenceChangeListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
		@Override
		public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
			if(key.equals(new String("use_gps"))) {
				if(preferences.getBoolean("use_gps",true)) {
					locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
				}
				else {
					locationManager = null;
				}
			}
			//else if(key.equals(new String("use_clf_db"))) {
			//	stateChangedNotification();
			//}
		}		
	};
}
